<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="消防员id" prop="firefighterId">
				<el-input v-model="queryParams.firefighterId" placeholder="请输入消防员id" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="消防员姓名" prop="firefighterName">
				<el-input v-model="queryParams.firefighterName" placeholder="请输入" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="消防员证件号" prop="firefighterIdCard">
				<el-input v-model="queryParams.firefighterIdCard" placeholder="请输入消防员证件号" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="adds = true"
					>新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['award:award:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['award:award:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['award:award:export']">导出</el-button>
			</el-col>
			<!-- <el-col :span="1.5">
				<el-button @click=" = true" size="mini" v-hasPermi="['award:award:export']">批量新增</el-button>
			</el-col> -->
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="awardList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="id" align="center" prop="id" />
			<el-table-column label="奖励情况" align="center" prop="awrad" />
			<el-table-column label="消防员id" align="center" prop="firefighterId" />
			<el-table-column label="消防员姓名" align="center" prop="firefighterName" />
			<el-table-column label="消防员证件号" align="center" prop="firefighterIdCard" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['award:award:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['award:award:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改请输入对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
			<el-form ref="form" :model="form" :rules="rules">
				<el-form-item label="奖励情况" prop="name">
					<el-input v-model="form.firefighterAwards[0].name" placeholder="请输入内容" />
				</el-form-item>

				<el-form-item label="时间" prop="time">
					<el-date-picker v-model="form.firefighterAwards[0].time" type="month" placeholder="选择月">
					</el-date-picker>
				</el-form-item>

				<el-form-item label="奖励等级" prop="levles">
					<el-select v-model="form.firefighterAwards[0].levles" placeholder="请选择">
						<el-option v-for="item in levelList" :key="item.rewardId" :label="item.rewardName"
							:value="item.rewardName">
						</el-option>
					</el-select>
				</el-form-item>

				

				<el-form-item label="消防员姓名" prop="firefighterName">
					<el-input v-model="form.firefighterName" placeholder="请输入" />
				</el-form-item>

				<el-form-item label="消防员证件号" prop="firefighterIdCard">
					<el-input v-model="form.firefighterIdCard" placeholder="请输入消防员证件号" @change="getFirefighterIdCard" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>

		<!-- 批量新增弹出层 -->
		<el-dialog title="批量新增" :visible.sync="adds" width="40%">
			<el-form ref="form" :model="addsForm" :rules="rules" label-width="" @submit.native.prevent>
				<div style="width: 100%; display: flex;justify-content: flex-end;padding-bottom: 20px;">
					<el-button @click="addInput">添加奖励输入框</el-button>
				</div>

				<el-form-item v-if="form.id == null">
					<span class="biaoti">奖励情况：</span>
					<div v-for="(item, i) in addsForm.firefighterAwards" :key="i">
						<div style="padding-bottom: 20px;padding-top: 20px;">
							<div style="display: flex;margin-bottom:20px ;">
							<span style="width: 10%;text-align: center;line-height: 40px;">时间</span>
							<el-date-picker @change="addsMonth(i, $event)" v-model="item.time" type="month"
								placeholder="选择月">
							</el-date-picker>
							</div>

							<div style="display: flex;margin-bottom:20px ;">
							<span style="width: 10%;text-align: center;line-height: 40px;">级别</span>
							<el-select v-model="item.levles" placeholder="请选择" @change="addsLevel(i,$event)">
								<el-option v-for="item in levelList" :key="item.rewardId" :label="item.rewardName"
									:value="item.rewardId">
								</el-option>
							</el-select>
							</div>

							<div style="display: flex;margin-bottom:20px ;">
							<span style="width: 10%;text-align: center;line-height: 40px;">奖励</span>
							<el-input :value="item.name" placeholder="请输入" @input="addsValue(i, $event)" />
							</div>
							</div>
					</div>
				</el-form-item>

				<el-form-item label="" prop="name">
					<span class="biaoti">消防员姓名</span>
					<el-input v-model="addsForm.firefighterName" placeholder="请输入" />
				</el-form-item>

				<el-form-item label="" prop="nameId">
					<span class="biaoti">消防员证件号</span>
					<el-input v-model="addsForm.firefighterIdCard" placeholder="请输入消防员证件号"
						@change="getFirefighterIdCard" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="addsUpdate">确 定</el-button>
				<el-button @click="adds = false">取 消</el-button>
			</div>
		</el-dialog>


	</div>
</template>

<script>
	import {
		listAward,
		getAward,
		delAward,
		addAward,
		updateAward,
		addAwardList,
		getRewardList,
		getFirefighterIdCard
	} from "@/api/award/award";

	export default {
		name: "Award",
		data() {
			return {
				levelList: [],
				addsForm: {
					firefighterName: "",
					firefighterIdCard: "",
					firefighterAwards: [{
						name: "",
						levles: "",
						time: ""
					}, ],
				},
				inputs: [], // 用于存储输入框的值
				adds: false,
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 奖励情况表格数据
				awardList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					awrad: null,
					firefighterId: null,
					firefighterName: null,
					firefighterIdCard: null
				},
				// 表单参数
				form: {
					firefighterName: "",
					firefighterIdCard: "",
					firefighterAwards: [{
						name: "",
						levles: "",
						time: ""
					}],
				},
				// 表单校验
				rules: {

				}
			};
		},
		created() {
			this.getList();

			this.getRewardList()
		},
		methods: {
			getFirefighterIdCard(nameId) {
				console.log("获取证件号：", nameId)
				getFirefighterIdCard(nameId).then(res => {
					console.log("证件号：", res)
					this.addsForm.firefighterName = res.data.name
					this.form.firefighterName = res.data.name
				})
			},
			// 荣誉称号列表
			getRewardList() {
				getRewardList().then(res => {
					console.log("荣誉称号列表:", res.data)
					this.levelList = res.data
				})
			},
			// 批量添加
			addsUpdate() {
				console.log("表单数据：", this.addsForm)
				addAwardList(this.addsForm).then(res => {
					console.log("返回结果：", res)
				})
				this.adds = false;
				this.getList();
			},
			addInput() {
				// 向inputs数组中添加一个新的输入框对象
				this.addsForm.firefighterAwards.push({
					time: "",
					levles: "",
					name: ""
				});
			},
			// 更新特定输入框的值
			addsValue(i, value) {
				console.log(value)
				this.addsForm.firefighterAwards[i].name = value;
			},
			addsLevel(i, level) {
				this.addsForm.firefighterAwards[i].levles = level;
			},

			addsMonth(i, e) {
				this.addsForm.firefighterAwards[i].time = this.MonthUpdate(e)
			},
			MonthUpdate(e) {
				var formattedDate = e;
				// 解析日期时间字符串为Date对象
				const date = new Date(formattedDate);
				// 获取年份
				const year = date.getFullYear();
				// 获取月份（0-11，所以要加1）
				const month = date.getMonth() + 1;
				// 格式化月份为两位数字
				const formattedMonth = month < 10 ? '0' + month : month;
				// 构建最终的日期字符串
				formattedDate = `${year}-${formattedMonth}`;
				console.log(formattedDate); // 输出: 2024-10
				return formattedDate
			},
			/** 查询奖励情况列表 */
			getList() {
				this.loading = true;
				listAward(this.queryParams).then(response => {
					this.awardList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: "",
					firefighterName: "",
					firefighterIdCard: "",
					firefighterAwards: [{
						name: "",
						levles: "",
						time: ""
					}],
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加奖励情况";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const id = row.id || this.ids
				getAward(id).then(response => {
					this.form.id = response.data.id;
					this.form.firefighterName = response.data.firefighterName;
					this.form.firefighterIdCard = response.data.firefighterIdCard;
					this.form.firefighterAwards[0].time = response.data.time;
					this.form.firefighterAwards[0].levles = response.data.levles;
					this.form.firefighterAwards[0].name = response.data.awrad;
					this.open = true;
					this.title = "修改奖励情况";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.form.firefighterAwards[0].time = this.MonthUpdate(this.form.firefighterAwards[0].time)
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.id != null) {
							console.log("修改表单数据：", this.form)
							updateAward(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							console.log("新增表单数据：", this.form)
							addAwardList(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$modal.confirm('是否确认删除奖励情况编号为"' + ids + '"的数据项？').then(function() {
					return delAward(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('award/award/export', {
					...this.queryParams
				}, `award_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>

<style>
	.biaoti {
		font-weight: bold;
		font-size: 20px;
	}
</style>